﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using NewRetail.Application.Components.WeChat.TemplateMessage.Model;
using NewRetail.Application.Components.WeChat.TemplateMessage.Services;
using NewRetail.Application.Core;
using SKIT.FlurlHttpClient.Wechat.Api.Models;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace NewRetail.Api.Controllers.WeChatControllers
{
    /// <summary>
    /// 微信发送模板消息
    /// </summary>
    [NewRetailRoute("wechat-templatemessage")]
    public class WeChatTemplateMessageController : BaseController
    {
        #region Field
        private readonly ILogger _logger;
        public readonly ITemplateMessageService _templateMessageService;
        #endregion

        #region Constructor
        public WeChatTemplateMessageController(ITemplateMessageService templateMessageService, ILogger<WeChatTemplateMessageController> logger)
        {
            _templateMessageService = templateMessageService;
            _logger = logger;
        }
        #endregion

        #region method

        #region 发送微信模板消息
        /// <summary>
        /// 发送微信模板消息
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        [HttpPost("wechat-send-basetemplatemessage")]
        public async Task<ResponseEntity<string>> SendWeChatBaseTemplateMessage([FromBody] TemplateMsgInputDto inputDto)
        {
            var ret = new ResponseEntity<string>();
            try
            {
                var appId = inputDto.AppId;
                var openId = inputDto.OpenId;
                var templateId = inputDto.TemplateId;
                var first = inputDto.First;
                var remark = inputDto.Remark;
                //发送消息的数据
                var messageData = new Dictionary<string, CgibinMessageTemplateSendRequest.Types.DataItem>();
                messageData.Add("first", new CgibinMessageTemplateSendRequest.Types.DataItem() { Value = first, Color = "#173177" });
                foreach (var item in inputDto.DataItem)
                {
                    messageData.Add(item.Key, new CgibinMessageTemplateSendRequest.Types.DataItem() { Value = item.Value });
                }
                messageData.Add("remark", new CgibinMessageTemplateSendRequest.Types.DataItem() { Value = remark });
                var templateMsgSendData = new TemplateMsgSendDto()
                {
                    AppId = appId,
                    OpenId = openId,
                    TemplateId = templateId,
                    MessageItemDate = messageData
                };
                //请求数据
                var result = await _templateMessageService.Send(templateMsgSendData);
                ret.SetData(result);
            }
            catch (Exception e)
            {
                ret.SetError("发送模板消息失败！" + e.Message);
                _logger.LogError(e.Message);
            }
            return ret;
        }
        #endregion

        #region 发送微信购物、关注等模板消息
        /// <summary>
        /// 发送微信购物、关注等模板消息
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        [HttpPost("wechat-send-templatemessage")]
        public async Task<ResponseEntity<string>> SendWeChatTemplateMessage([FromBody] TemplateMsgByTypeInputDto inputDto)
        {
            var ret = new ResponseEntity<string>();
            try
            {
                var openId = inputDto.OpenId;
                var messageType = inputDto.MessageType;
                //var first = inputDto.First;
                //var remark = inputDto.Remark;
                //发送消息的数据
                var messageData = new Dictionary<string, CgibinMessageTemplateSendRequest.Types.DataItem>();
                //messageData.Add("first", new CgibinMessageTemplateSendRequest.Types.DataItem() { Value = first, Color = "#173177" });
                foreach (var item in inputDto.DataItem)
                {
                    messageData.Add(item.Key, new CgibinMessageTemplateSendRequest.Types.DataItem() { Value = item.Value });
                }
                //messageData.Add("remark", new CgibinMessageTemplateSendRequest.Types.DataItem() { Value = remark });

                var templateMsgSendData = new TemplateMsgSendDto()
                {
                    MessageType = messageType,
                    OpenId = openId,
                    MessageItemDate = messageData
                };
                //请求数据
                var result = await _templateMessageService.Send(templateMsgSendData);
                ret.SetData(result);
                _logger.LogInformation("发送模板消息返回值：" + result);
            }
            catch (Exception e)
            {
                ret.SetError("发送模板消息失败！" + e.Message);
                _logger.LogError(e.Message);
            }
            return ret;
        }
        #endregion

        #endregion
    }
}
